package controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import service.MovieInfoService;
import domain.Movies;

@Controller
@RequestMapping("VIEW")
public class UpdateMovieController {
	
	@ModelAttribute("moviecommand")
	  protected Movies getMovieCommand(PortletRequest request) throws Exception {
		Movies command = new Movies() ;
	    return command;
	  }
	
	@RenderMapping(params = "action=updateMovie")
	  protected ModelAndView handleRender(@ModelAttribute(value="moviecommand")Movies command,
	      RenderRequest request, 
	      RenderResponse response) 
	      throws Exception {
	    
		Movies movie = command;
		movie.setId(Integer.parseInt(request.getParameter("movieId")));
		movieInfoService.addMovie(movie);
		
	    List moviesList = movieInfoService.getMovies();
		Map<String, Object> model1 = new HashMap<String, Object>();
		model1.put("moviesList", moviesList);
		return new ModelAndView("view", "moviesList", moviesList);
	  }

	private MovieInfoService movieInfoService;

	@Resource(name = "movieService")
	@Required
	public void setMovieService(MovieInfoService movieInfoService) {
		this.movieInfoService = movieInfoService;
	}
	
}
	
